/**
 * 
 */
package idv.takeshi.software.productline.bookstore.presentation.web.tag;

import idv.takeshi.software.productline.bookstore.domain.model.category.Category;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Show sidebar categories to jsp page.
 * @author takeshi
 *
 */
public class SidebarCategoriesTagHandler extends AbstractSimpleTagSupport {
	
	private static final Log log = LogFactory.getLog(SidebarCategoriesTagHandler.class);
	
	private static final String INDENT_STRING = "\t";
	
	private Category root;
	
	private Long selectedCategoryId;
	
	/**
	 * a uri with parameter for user selecting menubar. 
	 */
	private String uri;
	
	private String id;
	
	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
	 */
	@Override
	public void doTag() throws JspException, IOException {
		super.doTag();
		JspWriter writer = this.getJspContext().getOut();
		writer.println("<div class=\"sidebarmenu\">");
		writer.println("<ul id=\"" + this.id + "\">");
		for(Category subCategory : this.root.getSubCategories()){
			printCategories(writer, INDENT_STRING, subCategory);
		}
		writer.println("</ul>");
		writer.println("</div>");
		
	}
	
	
	private void printCategories(JspWriter writer, String indentString, Category category) 
		throws IOException{
		try {
			writer.println(indentString + "<li>");
			if(category.getId().equals(this.selectedCategoryId)){
				writer.print(indentString + "<a class=\"selected\" ");
			} else{
				writer.print(indentString + "<a ");
			}
			writer.print("href=\"" + this.uri + category.getId() + "\">");
			writer.print(category.getName().getText(getLocaleString()));
			writer.println(indentString + "</a>");
			
			if(category.hasSubCategories()){
				indentString += indentString;
				writer.println(indentString + "<ul>");
				for(Category subCategory : category.getSubCategories()){
					printCategories(writer, indentString, subCategory);
				}
				writer.println(indentString + "</ul>");
			}
			writer.println(indentString + "</li>");
		} catch (IOException e) {
			String msg = "generate categories content error";
			log.error(msg, e);
			throw e;
		}
		
	}

	/**
	 * @return the selectedCategoryId
	 */
	public Long getSelectedCategoryId() {
		return selectedCategoryId;
	}

	/**
	 * @param selectedCategoryId the selectedCategoryId to set
	 */
	public void setSelectedCategoryId(Long selectedCategoryId) {
		this.selectedCategoryId = selectedCategoryId;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the root
	 */
	public Category getRoot() {
		return root;
	}

	/**
	 * @param root the root to set
	 */
	public void setRoot(Category root) {
		this.root = root;
	}

	/**
	 * @return the uri
	 */
	public String getUri() {
		return uri;
	}

	/**
	 * @param uri the uri to set
	 */
	public void setUri(String uri) {
		this.uri = uri;
	}

	
	
	

}
